package org.example.fastproject.core.generator;

import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.dialect.Props;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;
import java.util.Scanner;

/**
 * @Author 王海舟
 * @Date 2022/12/28 15:05
 * @PackageName:org.example.store.common.mybatisplus
 * @ClassName: MyBatisPlusGenerator
 * @Description: 代码生成器
 * @Version 1.0
 */
public class MyBatisPlusGenerator {
    /**
     * 执行生成代码
     */
    public static void main(String[] args) {
        Props props = new Props("generator.properties");
        String url = props.getStr("dataSource.url");
        String username = props.getStr("dataSource.username");
        String password = props.getStr("dataSource.password");
        String outputDir = System.getProperty("user.dir") + "/src/main/java";

        String packageName = props.getStr("package.base");
        String moduleName = scanner("模块名");
        String[] tableNames = scanner("表名，多个英文逗号分割").split(",");
        String mapperDir = System.getProperty("user.dir") + "/src/main/resources/mapper/" + moduleName;
        // 代码生成器
        FastAutoGenerator
                //数据源
                .create(url,username,password)
                //全局配置
                .globalConfig(builder -> {
                    builder.outputDir(outputDir)
                            .author("王海舟")
                            .enableSwagger()//开启swagger
                            .dateType(DateType.ONLY_DATE)//时间类型为Date
                            .commentDate("yyyy-MM-dd HH:mm:ss");
                })
                //包配置
                .packageConfig(builder -> {
                    builder
                            .parent(packageName)
                            .moduleName(moduleName)
                            .entity("model")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .controller("controller")
                            .other("other")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, mapperDir));
                })
                //策略配置
                .strategyConfig(builder -> {
                    builder.addInclude(tableNames)
                            //entity策略
                            .entityBuilder()
                            .enableLombok()
                            //service策略
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            //mapper策略
                            .mapperBuilder()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper")
                            //controller策略
                            .controllerBuilder()
                            .enableRestStyle()
                            .build();
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    /**
     * 读取控制台内容信息
     */
    private static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(("请输入" + tip + "："));
        if (scanner.hasNext()) {
            String next = scanner.next();
            if (StrUtil.isNotEmpty(next)) {
                return next;
            }
        }
        throw new RuntimeException("请输入正确的" + tip + "！");
    }
}
